#include<stdio.h>
#include "../include/f3.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
//#include <time.h>
#define SERVPORT 3333	/*服务器监听端口号 */
#define MAXDATASIZE 10240 /*每次最大数据传输量 */
int sendmeg(char srv[])
{
	int sockfd,i=0;
	char buf[MAXDATASIZE],tmp[2]="a",cmd[50]="ps aux|sort -nr -k 3|head -10";
	char hostname[50],tmstr[20];
	time_t tm;
	gethostname(hostname,50);
	struct hostent *host;
	struct sockaddr_in serv_addr;
	if (0)
	{
	fprintf(stderr,"Please enter the server's hostname!\n");
	exit(1);
	}
	if((host=gethostbyname(srv))==NULL)
	{
		herror("gethostbyname出错！");
		exit(1);
	}
	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket创建出错！");
		exit(1);
	}
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(SERVPORT);
	serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
	bzero(&(serv_addr.sin_zero),8);
	if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1)
	{
		perror("connect出错！");
		exit(1);
	}
	while(!kbhit())
	{
		
		getinfo(buf,cmd);
		strcat(buf,"|||");
		strcat(buf,hostname);
		strcat(buf,"|||");
		time(&tm);
		li2str(tm,tmstr);
		strcat(buf,tmstr);
		
		send(sockfd, buf, strlen(buf)+1, 0);
//		send(sockfd, "123456", 7, 0);
		sleep(2);
		printf("%d\n",++i);
	}
	send(sockfd, "end", 7, 0);
	close(sockfd);
	return 0;
}

int main(int argc,char *argv[])
{
	sendmeg(argv[1]);
	return 0;
}
